package com.hbmcc.heshenghuo.view

import android.content.Context
import com.hbmcc.heshenghuo.functions.utils.DeviceUtil.dp2px
import android.widget.RelativeLayout
import android.widget.TextView
import android.view.LayoutInflater
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import com.hbmcc.heshenghuo.R
import com.hbmcc.heshenghuo.functions.glide.Img
import com.hbmcc.heshenghuo.model.http.bean.TitleAndUrlBean

/**
 * Copyright (c) 2020
 * 欣网互联网络科技有限公司
 * author: whs
 * created on: 2020/4/29 9:06
 */
class FloorTitle : RelativeLayout {

    /**
     * 是否居中  默认居左
     */
    private var isCenter = false

    private lateinit var tvTitle: TextView
    private lateinit var tvSubtitle: TextView
    private lateinit var tvMore: TextView
    private lateinit var main: RelativeLayout
    private var moreUrl: String? = ""

    private lateinit var ctrImgMain: View
    private lateinit var tvtTitleImg: TextView
    private lateinit var tvSubtitleImg: TextView
    private lateinit var tvMoreImg: TextView
    private lateinit var viewMoreImg: View
    private lateinit var imMoreIcon: ImageView
    private lateinit var imgTitleIconImg: ImageView

    private lateinit var ctrImgMainTitle: View
    private lateinit var imTitleImgTitle: ImageView
    private lateinit var tvSubtitleImgTitle: TextView
    private lateinit var viewMoreImgTitle: View
    private lateinit var tvMoreImgTitle: TextView
    private lateinit var imMoreIconTitle: ImageView

    /**
     * channel 频道  SY:通讯频道、SH：生活频道、WD：我的频道
     */
    var channel: String? = null

    var titleAndUrlBean: TitleAndUrlBean? = null

    var moreClickListener: MoreClickListener? = null

    constructor(context: Context?) : super(context)
    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet) {
        init(context, attributeSet)
    }

    constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int) : super(context, attributeSet, defStyle) {
        init(context, attributeSet)
    }

    fun init(context: Context, attributeSet: AttributeSet?) {
        LayoutInflater.from(context).inflate(R.layout.floor_title, this)
        tvTitle = findViewById(R.id.title)
        tvSubtitle = findViewById(R.id.subtitle)
        tvMore = findViewById(R.id.more)
        main = findViewById(R.id.main)

        tvtTitleImg = findViewById(R.id.tv_title_img)
        tvSubtitleImg = findViewById(R.id.tv_ful_title_img)
        ctrImgMain = findViewById(R.id.ctr_img_main)
        tvMoreImg = findViewById(R.id.tv_more_img)
        viewMoreImg = findViewById(R.id.view_more_bg_img)
        imMoreIcon = findViewById(R.id.im_more_icon)
        imgTitleIconImg = findViewById(R.id.img_title_icon_img)

        ctrImgMainTitle = findViewById(R.id.ctr_img_title_main)
        imTitleImgTitle = findViewById(R.id.img_title_img_title)
        tvSubtitleImgTitle = findViewById(R.id.tv_ful_title_img_title)
        viewMoreImgTitle = findViewById(R.id.view_more_bg_img_title)
        tvMoreImgTitle = findViewById(R.id.tv_more_img_title)
        imMoreIconTitle = findViewById(R.id.im_more_icon_title)

        val a = context.obtainStyledAttributes(attributeSet, R.styleable.FoundFloorTitle)
        tvTitle.text = a.getString(R.styleable.FoundFloorTitle_titleText) ?: ""
        tvTitle.textSize = a.getDimension(R.styleable.FoundFloorTitle_titleTextSize, 20f)
        setSubtitleText(a.getString(R.styleable.FoundFloorTitle_subtitleText))
        setSubtitleSize(a.getDimension(R.styleable.FoundFloorTitle_subtitleTextSize, 11f))
        setShow(a.getBoolean(R.styleable.FoundFloorTitle_isShow, true))
        setCenter(a.getBoolean(R.styleable.FoundFloorTitle_isCenter, isCenter))
        setShowMore(a.getBoolean(R.styleable.FoundFloorTitle_isShowMore, false))
        tvTitle.paint.isFakeBoldText = a.getBoolean(R.styleable.FoundFloorTitle_titleIsBold, true)
        a.recycle()

        //更多点击事件
        tvMore.setOnClickListener {
            if (moreClickListener == null) onMoreClick() else moreClickListener!!.onMoreClick()
        }
        viewMoreImg.setOnClickListener {
            if (moreClickListener == null) onMoreClick() else moreClickListener!!.onMoreClick()
        }
        viewMoreImgTitle.setOnClickListener {
            if (moreClickListener == null) onMoreClick() else moreClickListener!!.onMoreClick()
        }
    }

    private fun setShow(show: Boolean) {
        if (show) {
            main.visibility = VISIBLE
            ctrImgMain.visibility = View.GONE
        } else {
            main.visibility = GONE
            ctrImgMain.visibility = View.GONE
        }
    }

    fun setCenter(center: Boolean) {
        isCenter = center
        if (center) { //隐藏副标题
            tvSubtitle.visibility = GONE
            val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
            layoutParams.addRule(CENTER_IN_PARENT)
            tvTitle.layoutParams = layoutParams
        } else {
            tvSubtitle.visibility = VISIBLE
            val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
            layoutParams.addRule(CENTER_VERTICAL)
            layoutParams.marginStart = dp2px(7f)
            tvTitle.layoutParams = layoutParams
        }
    }

    private fun setShowMore(showMore: Boolean) {
        tvMore.visibility = if (showMore) VISIBLE else GONE
    }

    private fun setSubtitleText(subtitleText: String?) {
        if (!isCenter) tvSubtitle.text = subtitleText ?: ""
    }

    fun setHeight(height: Int) {
        val params = main.layoutParams as LayoutParams
        params.height = dp2px(height.toFloat())
        main.layoutParams = params
    }

    fun setHeight(height: Int, marginTop: Int) {
        val params = main.layoutParams as LayoutParams
        params.height = dp2px(height.toFloat())
        params.topMargin = dp2px(marginTop.toFloat())
        main.layoutParams = params
    }

    private fun setMoreUrl(moreUrl: String?) {
        this.moreUrl = moreUrl
    }

    fun setTitleSize(titleSize: Float) {
        tvTitle.textSize = titleSize
    }

    private fun setSubtitleSize(subtitleSize: Float) {
        tvSubtitle.textSize = subtitleSize
    }

    /**
     * 传入titleBean
     */
    fun setBean(bean: TitleAndUrlBean?) {
        if (bean == null) {
            setShow(false)
            return
        }

        titleAndUrlBean = bean

        if ("1" != bean.isTitle) {  //是否显示
            setShow(false)
            return
        }

        when (bean.titleType) {
            0 -> {
                main.visibility = VISIBLE
                ctrImgMain.visibility = View.GONE
                ctrImgMainTitle.visibility = View.GONE
                setCenter("1" == bean.floorSeat)  //是否居中
                tvTitle.text = bean.name ?: "" //设置主标题
                setSubtitleText(bean.fuTitle ?: "") //设置副标题
                tvMore.text = bean.moreName ?: "" //设置更多
                if (bean.moreUrl.isNullOrEmpty()) setShowMore(false) //是否显示更多
                else {
                    setShowMore(true) //设置更多URL
                    setMoreUrl(bean.moreUrl)
                }
            }
            1 -> {
                main.visibility = GONE
                ctrImgMainTitle.visibility = GONE
                ctrImgMain.visibility = View.VISIBLE

                tvtTitleImg.text = bean.name ?: "" //设置主标题
                tvSubtitleImg.text = bean.fuTitle ?: ""
                if (bean.moreUrl.isNullOrEmpty()) {
                    tvMoreImg.visibility = GONE
                    viewMoreImg.visibility = GONE
                    imMoreIcon.visibility = GONE
                } else {
                    tvMoreImg.visibility = VISIBLE
                    viewMoreImg.visibility = VISIBLE
                    imMoreIcon.visibility = VISIBLE
                    tvMoreImg.text = bean.moreName ?: ""
                    Img.with(context).load(bean.titleIconUrl).placeholder(R.mipmap.floor_default).into(imgTitleIconImg)
                }
            }
            2 -> {
                main.visibility = GONE
                ctrImgMainTitle.visibility = VISIBLE
                ctrImgMain.visibility = View.GONE

                Img.with(context).load(bean.titleIconUrl).placeholder(R.mipmap.floor_default).into(imTitleImgTitle)

                tvSubtitleImgTitle.visibility = if (bean.fuTitle.isNullOrEmpty()) View.GONE
                else {
                    tvSubtitleImgTitle.text = bean.fuTitle
                    View.VISIBLE
                }


                if (bean.moreUrl.isNullOrEmpty()) {
                    viewMoreImgTitle.visibility = GONE
                    tvMoreImgTitle.visibility = GONE
                    imMoreIconTitle.visibility = GONE
                } else {
                    viewMoreImgTitle.visibility = VISIBLE
                    tvMoreImgTitle.visibility = VISIBLE
                    imMoreIconTitle.visibility = VISIBLE
                    tvMoreImgTitle.text = bean.moreName ?: ""
                }
            }
        }


    }

    private fun onMoreClick() {
        if (moreUrl.isNullOrEmpty()) return // addUserAction(titleAndUrlBean, channel)
        //PageManager.build(moreUrl).with(Page.pointsCode, channel).go(context)
    }


}

/**
 * 点击更多的点击
 */
interface MoreClickListener {
    fun onMoreClick()
}